package jeecg.mongodb.db;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jeecg.mongodb.system.BeanFactory;
import jeecg.mongodb.system.DBFileSystem;
import jeecg.mongodb.system.DataBase;
import jeecg.mongodb.system.DynamicBean;

public class DocumentOpration extends Opration {
	
	public String insert(DataBase db,String document,String value){
		FileWriter writer=null;
		try {
			String path=DBFileSystem.getDocumentPath(db, document);
			File file=new File(path);
			if(!file.exists()){
				writer = new FileWriter(file, true);
				file.createNewFile();
				writer.write("{}");  
				writer.write("\r\n"+value);  
			}else{
				writer = new FileWriter(file, true);
				writer.write("\r\n"+value);  
			}
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	public String delete(DataBase db,String document,String value) {
		DynamicBean dynamicBean=BeanFactory.createQueryBean(value);
		String path=DBFileSystem.getDocumentPath(db, document);
		File file=new File(path);
		File file2=new File(path.replaceFirst(".txt", "1.txt"));
		try {
			DBFileSystem.copyFileDelete(file,file2,dynamicBean);
			DBFileSystem.copyFile(file2,file,false);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		file2.delete();
		return "success";
	}
	
	public String update(DataBase db,String document,String value,String update){
		DynamicBean dynamicBean=BeanFactory.createQueryBean(value);
		String path=DBFileSystem.getDocumentPath(db, document);
		File file=new File(path);
		File file2=new File(path.replaceFirst(".txt", "1.txt"));
		try {
			DBFileSystem.copyFileUpdate(file,file2,dynamicBean,update);
			DBFileSystem.copyFile(file2,file,false);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		file2.delete();
		return "success";
	}
	
	@SuppressWarnings("unused")
	public List<DynamicBean> query(DataBase db,String document,String value){
		//目标查找对象
		DynamicBean dynamicBean=BeanFactory.createQueryBean(value);
		//集合list
		List<DynamicBean> list=new ArrayList<DynamicBean>();
		
		String path=DBFileSystem.getDocumentPath(db, document);
		File file=new File(path);
		if(!file.exists()){
			return null;
		}
		try {
			FileReader reader = new FileReader(file);
			BufferedReader br = new BufferedReader(reader);
			String str = null;
			while(( str = br.readLine()) != null) {
				DynamicBean dbean=DBFileSystem.compareObject(dynamicBean,str,true);
				if(dbean!=null){
					list.add(dbean);
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;

	}
}




